package com.example.orderfood2.managerOperate;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.orderfood2.DatabaseManager.DatabaseManager;
import com.example.orderfood2.MainActivity;
import com.example.orderfood2.R;
import com.example.orderfood2.Repository.TeacherRepository;
import com.example.orderfood2.entity.Teacher;
import com.example.orderfood2.util.DBUtil;
import com.example.orderfood2.util.ToolsUtil;


//管理员添加老师
public class AddTeacherActivity extends AppCompatActivity {
    private int year, month, day;

    private String sex = "男", selectedValue;
    private TeacherRepository dbUtil;
    private Teacher teacher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //创建数据库
        dbUtil= DatabaseManager.getInstance(this).getTeacherRepository();
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_add_teacher);
        //添加选择日期的代码
        TextView teacher_date = findViewById(R.id.teacher_date);
        teacher_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog();
            }
        });
        //获取各输入框
        EditText teacher_username = findViewById(R.id.et_teacher_username);
        EditText teacher_password = findViewById(R.id.et_teacher_password);
        RadioGroup teacher_sex = findViewById(R.id.teacher_sex);
        Spinner spinner_teacher_title = findViewById(R.id.spinner_teacher_title);
        EditText teacher_science = findViewById(R.id.et_teacher_science);
        // 设置 RadioGroup 的选中状态改变监听器
        teacher_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                // 获取选中的 RadioButton 实例
                RadioButton radioButton = findViewById(checkedId);
                sex = radioButton.getText().toString();

            }

        });
//     设置老师教职的选中状态改变监听器
        spinner_teacher_title.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 获取选中项的值
                selectedValue = (String) parent.getItemAtPosition(position);

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                Toast.makeText(AddTeacherActivity.this, "请选择老师的职称", Toast.LENGTH_SHORT).show();
            }
        });
        //提交表单
        Button teacher_submit = findViewById(R.id.teacher_submit);
        teacher_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //         拼接日期
                String date = year + "-" + month + "-" + day;
                //转换为字符串
                String username = ToolsUtil.editTextToString(teacher_username);
                String password = ToolsUtil.editTextToString(teacher_password);
                String science = ToolsUtil.editTextToString(teacher_science);
//                添加老师
                if (password.isEmpty() || username.isEmpty() || science.isEmpty() || date.equals("0-0-0")) {
                    Toast.makeText(AddTeacherActivity.this, "请完整填写表单", Toast.LENGTH_SHORT).show();
                } else {
                    //创建老师实体类
                    teacher = new Teacher(null, username, password, sex, selectedValue, date, science);
                    //调用DBUtil添加老师
                    long result=dbUtil.registerTeacher(teacher);
                    if(result!=-1){
                        Toast.makeText(AddTeacherActivity.this, "老师:"+username+"添加成功", Toast.LENGTH_SHORT).show();
                        teacher_username.setText("");
                        teacher_password.setText("");
                        teacher_science.setText("");
                        teacher_date.setText("请选择日期");
                    }else {
                        Toast.makeText(AddTeacherActivity.this, "添加失败，请重试", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


    }

    private void showDatePickerDialog() {
        //添加选择日期的代码
        TextView teacher_date = findViewById(R.id.teacher_date);
        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        //// 使用DatePickerDialog组件
        DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @SuppressLint("SetTextI18n")
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// 格式化日期（如：yyyy-MM-dd）
                        teacher_date.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
                        AddTeacherActivity.this.year = year;
                        AddTeacherActivity.this.month = month;
                        AddTeacherActivity.this.day = dayOfMonth;
                    }
                },
//                下面3个参数是对话框初始化时填入的参数
                year,
                month,
                day);
        datePickerDialog.show();
    }
}
